<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>函数默认值</h1>
    <p>ES5传统方式： a = a || 值; 该方式有歧义，当a的值是null,undefined,或者0值（0,"",false）时判断都会生效。</p>
    <p>ES6的方式：仅在值为undefined时会生效。</p>
    <script>
        
        function sum(a,b) {
            // 采用ES5的这种运算方式设置默认值：null/undefined都会生效
            // 此时，使用数值影响好像不太大
            a = a || 0;
            b = b || 0;
            return a + b;
        }

        function sayHello(x,y) {
            // 如果y是空字符串（""），它也会判断生效。
            y = y || 'World';
            console.log(x,y);
        }

        function chengfa(a,b) {
            // 如果是数值，0值也会生效
            a = a || 1;
            b = b || 1;
            console.log("a * b = ",a * b);
        }



        let result = sum();
        console.log("result = ",result); // 0

        result = sum(null,null);
        console.log("result = ",result); // 0

        sayHello("Hello","World"); // Hello World
        sayHello("Hello",null); // Hello World
        sayHello("Hello"); // Hello World
        sayHello("Hello",""); // Hello World

        chengfa(1,0); // 1


        function sayHelloES6(x = "Hello",y = "World") {
            console.log(x,y);
        }

        sayHelloES6(); // Hello World
        sayHelloES6(null,null); // null null
        sayHelloES6(undefined,""); // Hello ""

    </script>
</body>
</html>